package wpmp.security.mgr.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import wpmp.extjs3.data.Combo;
import wpmp.extjs3.data.JsonStore;
import wpmp.security.client.utils.CodeUtils;
import wpmp.utils.quickbean.Code;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 2:01:19 PM Dec 4, 2013
 */
public class GetComboStoreServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private final Log log = LogFactory.getLog(getClass());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/json;charset=UTF-8");
		final String sortType = req.getParameter("sortType");
		if (StringUtils.isBlank(sortType)) {
			resp.getWriter().write(JsonStore.EMPTY.toJSONString());
			return;
		}

		final List<Combo> records = new ArrayList<Combo>();
		for (Code code : CodeUtils.get(sortType)) {
			records.add(new Combo(code.getText(), code.getValue()));
		}

		String result = new JsonStore<Combo>(records).toJSONString();

		if (log.isDebugEnabled()) {
			log.debug(result);
		}

		resp.getWriter().write(result);
	}
}
